import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Vant from "vant";
import "vant/lib/index.css";
import { Notify } from "vant";
import { Dialog } from "vant";
import "amfe-flexible";
import "@/assets/style/common.less";
import plugins from "@/plugins/index.js";
import { getCode } from "@/api/isc.js";
import "@/assets/js/vconsole.js"; // 调试面板
import commonReg from "@/assets/js/reg";
import Setting from "./setting";
import infoHiding from "@/plugins/utils/infoHiding.js";
import VueDOMPurifyHTML from "vue-dompurify-html";

Vue.config.productionTip = false;
Vue.use(Vant).use(plugins).use(VueDOMPurifyHTML);
let priCenter = ["0424AA84873ABC47A75FE01", "fda7924bdf1a35b278343e8c"];
window.SM_KEY =
  priCenter[0] +
  commonReg.smPart +
  window.smMid +
  commonReg.smScene +
  window.smFinish;
// 提示确认框
Vue.prototype.$Notify = Notify;
Vue.prototype.$Dialog = Dialog;
Vue.prototype.$commonReg = commonReg; // 正则定义
Vue.prototype.$infoHiding = infoHiding; // 脱敏
window.PRI_CONSTANT = window.priBegin + priCenter[1] + commonReg.priEnd;

function getParams() {
  var params = {};
  var _index = location.href.indexOf("?");
  if (_index > -1) {
    var paramArray = unescape(location.href)
      .substring(_index + 1, location.href.length)
      .split("&");
    if (paramArray.length > 0) {
      paramArray.forEach(function (currentValue) {
        var text = currentValue.split("=")[1];
        if (text && text.indexOf("#/") != -1) {
          text = text.slice(0, text.indexOf("#/"));
        }
        params[currentValue.split("=")[0]] = text;
      });
    }
  }
  return params;
}

// 路由拦截
router.beforeEach(async (to, from, next) => {
  if (Setting.codeMode == "develop") {
    next();
    return;
  }
  if (!sessionStorage.getItem("userInfo") && to.path !== "/welcome") {
    // 获取appCode
    let appCode = getParams().code;
    if (!appCode) {
      getCode();
    } else {
      next({ name: "welcome", params: { appCode } });
    }
  } else if (sessionStorage.getItem("userInfo") && to.path === "/welcome") {
    next("/");
  } else {
    next();
  }
});

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
